<?php
class Sys_region_model extends CI_Model {

	public function __construct(){
		parent::__construct();
		$this->db = $this->load->database('default',true);
	}

	/**
	 * 查询区域列表
	 * @param string $parent_id
	 */
	public function  query_regions($parent_id) {
		$sql ="select * from sys_region where region_status ='1'and  parent_id ='$parent_id' order by region_id asc";
		$query	=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->result_array();
		}
	}

	public function query_parent_id($region_id){
		$sql ="select parent_id from sys_region where region_status ='1'and  region_id ='$region_id' limit 1";
		$query	=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->row_array();
		}
	}

	public function query_region_info($region_id){
		$sql ="select * from  sys_region where region_status ='1'and  region_id ='$region_id' limit 1";
		$query	=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->row_array();
		}
	}

	public function query_region_info_by_name($region_name){
		$sql ="select * from  sys_region where region_status ='1'and  region_name ='$region_name' limit 1";
		$query	=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->row_array();
		}
	}
	public function insert_region($region){
		$region['region_status']=1;
		$this->db->insert('sys_region',$region);
	}
	public function update_region($region,$region_id){
		$this->db->where('region_id',$region_id);
		$this->db->update('sys_region',$region);
	}

	public function set_delete_region($region_id){
		$sql ="update sys_region set region_status ='0' where region_id='$region_id'";
		$this->db->query($sql);
	}
	public function query_user_top_regions($user_id){
		$sql ="select sys_region.region_id,region_name,region_type from sys_region 
				inner join adm_user_region on sys_region.region_id =adm_user_region.region_id 
				where 
				region_status ='1'
				and user_id='$user_id' 
				and  parent_id ='0' 
				order by region_id asc";
		$query	=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->result_array();
		}
	}
}
?>